package com.xiyuan.smartutils.template.engine.expression.operator;

import com.xiyuan.smartutils.Types;

import com.xiyuan.smartutils.template.engine.VariableMap;
import com.xiyuan.smartutils.template.engine.exception.ExpressionException;
import com.xiyuan.smartutils.template.engine.expression.Expression;

/**
 * 数字数组，双点号运算符(..)
 * 
 * @version v1.0.0 @author lgz 2014-3-21 新建与整理
 */
public class _NumberArray extends Operator
{
    private static final long serialVersionUID = 1L;
    private final Expression left;
    private final Expression right;
    
    public _NumberArray(String path, int line, int column, Expression left, Expression right)
    {
        super(path, line, column);
        this.left = left;
        this.right = right;
        this.assertSymbo("{..}", left, right);
    }
    
    @Override
    public int getType()
    {
        return NUMBER_ARR;
    }
    
    @Override
    protected Object build(VariableMap variableMap)
    {
        Object start = left.execute(variableMap);
        if (!Types.isNumber(start))
            throw new ExpressionException(left, "数组表达式{" + this + "}格式不正确，左边表达式返回结果不是整型数字");
        
        Object end = right.execute(variableMap);
        if (!Types.isNumber(end))
            throw new ExpressionException(right, "数组表达式{" + this + "}格式不正确，左边表达式返回结果不是整型数字");
        
        Number v1 = ((Number) start);
        Number v2 = ((Number) end);
        if (Types.isDecimal(v1) || Types.isDecimal(v2))
        {//返回小数
            double len = (v2.doubleValue() - v1.doubleValue() + 1);
            if (len <= 0)
                return new int[0];
            if (len > Integer.MAX_VALUE)
                throw new ExpressionException(this, "数组表达式{" + this + "}格式不正确，数组长度不允许超出 int 最大值");
            
            if (Types.isFloat(start) || Types.isFloat(end))
            {// float[]
                float[] arr = new float[(int) len];
                float s = v1.floatValue();
                for (int i = 0; i < arr.length; i++)
                    arr[i] = s + i;
                
                
                return arr;
            }
            
            
            double[] arr = new double[(int) len];
            double s = v1.floatValue();
            for (int i = 0; i < arr.length; i++)
                arr[i] = s + i;
            
            return arr;
        }
        
        long len = (v2.longValue() - v1.longValue() + 1);
        if (len <= 0)
            return new int[0];
        if (len > Integer.MAX_VALUE)
            throw new ExpressionException(this, "数组表达式{" + this + "}格式不正确，数组长度不允许超出 int 最大值");
        
        if (Types.isLong(start) || Types.isLong(end))
        {// long[]
            long[] arr = new long[(int) len];
            long s = v1.longValue();
            for (int i = 0; i < arr.length; i++)
                arr[i] = s + i;
            
            return arr;
        }
        
        //最後返回 int
        int[] arr = new int[(int) len];
        int s = v1.intValue();
        for (int i = 0; i < arr.length; i++)
            arr[i] = s + i;
        
        return arr;
    }
    
    @Override
    public String toString()
    {
        return new StringBuilder().append(left).append("..").append(right).toString();
    }
}
